/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.beans.bind;

import java.util.List;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;

/**
 * Provides a property editor registrar implementation that registers custom
 * editors  on a property editor registry from a list of registrars.
 * 
 * Typically, property editor registrars are designed to register only for a
 * specific set of custom property editors.  However, some classes, such
 * as Spring Web Flow actions, are not designed to support multiple registrars.
 * 
 * <p>
 * This multi-property editor registrar adapter does not register any property
 * editors itself, instead delegating property editor registration
 * to a given list of property editor registrars.
 * </p>
 * 
 * @author Scott Rossillo
 *
 */
public class MultiPropertyEditorRegistrarAdapter implements
		PropertyEditorRegistrar {
	
	private PropertyEditorRegistrar[] propertyEditorRegistrars;

	/**
	 * Registers custom property editors on the given property editor registry
	 * by calling <code>registerCustomEditors()</code> on all {@link PropertyEditorRegistrar}s
	 * registered with this registrar via {@link #setPropertyEditorRegistrars(List)}.
	 * 
	 * @see org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors(org.springframework.beans.PropertyEditorRegistry)
	 */
	public final void registerCustomEditors(PropertyEditorRegistry propertyEditorRegistry) {
	
		if(propertyEditorRegistrars == null) {
			return;
		}
		
		for(int i = 0; i < propertyEditorRegistrars.length; i++) {
			propertyEditorRegistrars[i].registerCustomEditors(propertyEditorRegistry);
		}

	}
	
	/**
	 * Sets the property editor registrars to invoke when
	 * {@link #registerCustomEditors(PropertyEditorRegistry)} is called
	 * on this property editor registrar.
	 *  
	 * @param propertyEditorRegistrars a <code>List</code> of
	 * <code>PropertyEditorRegistrar</code>s
	 */
	public void setPropertyEditorRegistrars(List propertyEditorRegistrars) {
		
		if(propertyEditorRegistrars == null) {
			return;
		}
		
		this.propertyEditorRegistrars = 
			(PropertyEditorRegistrar[]) propertyEditorRegistrars.toArray(new PropertyEditorRegistrar[0]);
	}

}
